#include "software_spi.h"

static void SPI_W_CS(uint8_t BitValue)
{
	SPI_IO_WritePin(CS_Port,CS_Pin,(GPIO_PinState) BitValue);
}

static void SPI_W_SCK(uint8_t BitValue)
{
	SPI_IO_WritePin(SCK_Port,SCK_Pin,(GPIO_PinState) BitValue);
}

static void SPI_W_MOSI(uint8_t BitValue)
{
	SPI_IO_WritePin(MOSI_Port,MOSI_Pin,(GPIO_PinState) BitValue);
}

static uint8_t SPI_R_MISO(void)
{
	return HAL_GPIO_ReadPin(MISO_Port,MISO_Pin);
}

void SPI_Init(void)
{
	SPI_W_CS(1);
	SPI_W_SCK(0);
}

void SPI_Start(void)
{
	SPI_W_CS(0);
}

void SPI_Stop(void)
{
	SPI_W_CS(1);
}

uint8_t SPI_SwapByte(uint8_t ByteSend)
{
	uint8_t i,ByteReceive = 0x00;
	for(i = 0;i < 8;i++)
	{
		SPI_W_MOSI(ByteSend & (0x80 >> i));
		SPI_W_SCK(1);
		if(SPI_R_MISO() == 1){ByteReceive |= (0x80 >> i);}
		SPI_W_SCK(0);
	}
	return ByteReceive;
}
/*
uint8_t SPI_SwapByte(uint8_t ByteSend)
{
	uint8_t i;
	for(i = 0;i < 8;i++)
	{
		SPI_W_MOSI(ByteSend & 0x80);
		ByteSend << 1;
		SPI_W_SCK(1);
		if(SPI_R_MISO() == 1){ByteSend |= 0x01;}
		SPI_W_SCK(0);
	}
	return ByteSend;
}
*/